﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace HQWeb.LibarySys
{
    public partial class OutLibrary : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                BindList();
            }
        }

        private void BindList()
        {
            int bid = int.Parse(Session["BRANCH_ID"].ToString());
            string titel = this.txtTitel.Text.Trim();
            if (d12.Value == "")
            {
                DateTime date = DateTime.Now;
                d12.Value = date.AddMonths(-1).ToShortDateString().Replace('/', '-');
            }
            DateTime begintime = Convert.ToDateTime(d12.Value);
            if (d13.Value == "")
            {
                DateTime date = DateTime.Now;
                d13.Value = date.ToShortDateString().Replace('/', '-');
            }
            DateTime endtime = Convert.ToDateTime(d13.Value).AddDays(1);
            int state = int.Parse(drop_State.SelectedValue);
            DataSet LibraryOut = new DataSet();
            BLL.LibrarySys.T_PUB_LIBRARYOUT bll = new BLL.LibrarySys.T_PUB_LIBRARYOUT();
            LibraryOut = bll.GetApplyList(-1, bid, titel, state, begintime, endtime);
            GridView1.DataSource = LibraryOut.Tables[0];
            GridView1.DataBind();
        }

        #region 翻页事件
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                //strWhere = strWhere.Replace("order by operatetime DESC", "");
                int newPageIndex = 0;

                if (-2 == e.NewPageIndex)
                { // when click the "GO" Button
                    TextBox txtNewPageIndex = null;
                    GridViewRow pagerRow = GridView1.Controls[0].Controls[GridView1.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                    //GridViewRow pagerRow = GridView1.BottomPagerRow; //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                    if (null != pagerRow)
                    {
                        txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                    }

                    if (null != txtNewPageIndex)
                    {
                        newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                    }

                    newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
                    newPageIndex = newPageIndex >= GridView1.PageCount ? GridView1.PageCount - 1 : newPageIndex;
                    GridView1.PageIndex = newPageIndex;
                }
                else if (e.NewPageIndex == -1)
                {
                    newPageIndex = 0;
                    GridView1.PageIndex = 0;
                }
                else
                {  // when click the first, last, previous and next Button
                    newPageIndex = e.NewPageIndex;
                    GridView1.PageIndex = e.NewPageIndex;
                }

                // check to prevent form the NewPageIndex out of the range


                BindList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 编辑删除

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strCommand = e.CommandName;
            string strID = e.CommandArgument.ToString();
            if (strCommand.Equals("EditData"))  //编辑
            {
                Response.Redirect("ViewOutApply.aspx?sign=4&id=" + strID);
            }
            else if (strCommand.Equals("View"))
            {
                Response.Redirect("ViewOutApply.aspx?sign=5&id=" + strID);
            }
            //int id = int.Parse(strID);
            //Model.LibrarySys.T_PUB_LIBRARYOUT libraryout = new Model.LibrarySys.T_PUB_LIBRARYOUT();
            //BLL.LibrarySys.T_PUB_LIBRARYOUT lbll = new BLL.LibrarySys.T_PUB_LIBRARYOUT();
            //Model.LibrarySys.T_PUB_LIBRARYMATERIAL material = new Model.LibrarySys.T_PUB_LIBRARYMATERIAL();
            //BLL.LibrarySys.T_PUB_LIBRARYMATERIAL mbbl = new BLL.LibrarySys.T_PUB_LIBRARYMATERIAL();
            //libraryout = lbll.GetModel(id);
            //int? bid = libraryout.BRANCH_ID;
            //int? mid = libraryout.MATERIAL_ID;
            //if (strCommand.Equals("EditData"))  //编辑
            //{
            //    if (mbbl.ExistsMaterial(bid, mid))
            //    {
            //        material = mbbl.GetModel(bid, mid);
            //        if (material.NUM >= libraryout.NUM)
            //        {
            //            DateTime date = DateTime.Now;
            //            material.NUM -= libraryout.NUM;
            //            libraryout.REAL_TIMEOUT = date;
            //            libraryout.STATE = "4";
            //            mbbl.UpdateMaterial(libraryout, material);
            //            BindList();
            //        }
            //        else
            //        {
            //            Universal.JS.Alert("库存量不足！请您及时采购！");
            //        }
            //    }
            //    else
            //    {
            //        Universal.JS.Alert("仓库不存在该物品！请您及时采购！");
            //    }
            //    //Response.Redirect("LibrayOutApply.aspx?sign=1&id=" + strID);
            //    //bll.UpdateOut(id, "4", date);
            //}
            //else if (strCommand.Equals("View"))
            //{
            //    //Response.Redirect("ViewOutApply.aspx?id=" + strID);
            //}
        }

        #endregion

        //查询
        protected void tbnSearch_Click(object sender, EventArgs e)
        {
            BindList();
        }

        protected string getSatte(string strType)
        {
            string str = "";
            switch (strType)
            {
                case "1": str = "待审批"; break;
                case "2": str = "审批未通过"; break;
                case "3": str = "待出库"; break;
                case "4": str = "已出库"; break;
                case "5": str = "已入库"; break;
                default:
                    break;
            }
            return str;
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int index = e.Row.RowIndex;
                DataRowView drv = (DataRowView)e.Row.DataItem;
                string strState = drv.Row["STATE"].ToString();
                LinkButton btn2 = (LinkButton)e.Row.Cells[6].FindControl("btnEdit");
                if (strState == "3")
                {
                    btn2.Text = "出 库";
                    btn2.CommandName = "EditData";
                }
                else if (strState == "4")
                {
                    btn2.Text = "已出库";
                    btn2.CommandName = "View";
                    //btn2.Enabled = false;
                }
            }
        }
    }
}